<?php
namespace Services\Api;
use Common\Models\BShopGoods;
use Common\Models\BShopGoodsSku;
use Common\Models\BShopOrderCar;
use Common\Models\BShopSkuValue;
use Services\ServicesApiBase;
class ShopOrderServices extends ServicesApiBase
{

    /**
     * 商品添加购物车
     */
    public function addGoodsCard($params){
        $BShopGoodsDb = new BShopGoods();
        $bShopGoods = $BShopGoodsDb->findFirst(array(
            "conditions"=>"goods_sn='{$params['goods_id']}' AND status=1 AND admin_status=1"
        ));
        if(!$bShopGoods){
            return ['status'=>false,'msg'=>'该商品已下架'];
        }
        $BShopGoodsSkuDb = new BShopGoodsSku();
        $BShopGoodsSku = $BShopGoodsSkuDb->findFirst(array(
            "id={$params['sku_id']}"
        ));
        if(!$BShopGoodsSku){
            return ['status'=>false,'msg'=>'没有找到商品规格'];
        }
        if($BShopGoodsSku->stock<=0){
            return ['status'=>false,'msg'=>'该商品库存不足'];
        }
        $value_ids = $BShopGoodsSku->value_ids;
        if(strpos($BShopGoodsSku->value_ids,'_') !== false){
            $value_ids = str_replace("_",',',$value_ids);
        }

        $BShopOrderCarDb = new BShopOrderCar();
        $BShopOrderCarDb = $BShopOrderCarDb->findFirst(array(   //查看当前商品是否已经添加了购物车 如果添加了 数量+1
            "conditions"=>"goods_id='{$bShopGoods->goods_sn}' AND goods_sku_ids='{$BShopGoodsSku->value_ids}' AND user_id={$params['user_id']}"
        ));
        if($BShopOrderCarDb){
            $BShopOrderCarDb->num += 1;
        }else{
            $BShopSkuValueDb = new BShopSkuValue();
            $getValueKeyName = $BShopSkuValueDb->getValueKeyName($value_ids);

            $BShopOrderCarDb = new BShopOrderCar();
            $BShopOrderCarDb->goods_id = $bShopGoods->goods_sn;
            $BShopOrderCarDb->shop_id = $bShopGoods->shop_id;
            $BShopOrderCarDb->user_id = $params['user_id'];
            $BShopOrderCarDb->num = $params['num'];
            $BShopOrderCarDb->goods_sku_ids = $BShopGoodsSku->value_ids;
            $BShopOrderCarDb->created = date("Y-m-d H:i:s");
            $BShopOrderCarDb->goods_name = $bShopGoods->name;
            $BShopOrderCarDb->goods_sku_name = $getValueKeyName['value_key_name'];
            $BShopOrderCarDb->detailed_img = $bShopGoods->detailed_img;
        }
        if($BShopOrderCarDb->save()){
            return ['status'=>true,'msg'=>'添加成功'];
        }
        return ['status'=>false,'msg'=>'添加失败'];
    }


    /**
     * 购物车列表
     */
    public function carList($params){
        $BShopOrderCarDb = new BShopOrderCar();
        $responseResult = $BShopOrderCarDb->getCarList($params);
        return $responseResult;
    }

    /**
     * 删除购物车
     */
    public function carDeleteGoods($params){
        $BShopOrderCarDb = new BShopOrderCar();
        $responseResult = $BShopOrderCarDb->find(array(
            'conditions'=>"id in({$params['ids']})"
        ));
        if($responseResult->delete()){
            return ['status'=>true,'msg'=>'删除成功'];
        }
        return ['status'=>false,'msg'=>'删除失败'];
    }

}